package com.example.studyapp


import android.content.Context
import android.content.Intent
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.graphics.drawable.toBitmap
import com.android.volley.toolbox.Volley

class testMyOvalImageView(context: Context, attrs: AttributeSet) : AppCompatImageView(
        context,
        attrs
) {


    private val mPaintBitmap: Paint = Paint(Paint.ANTI_ALIAS_FLAG)//抗锯齿
    private var mRawBitmap: Bitmap? = null
    private var mShader: BitmapShader? = null
    private val mMatrix: Matrix = Matrix()
    private var strokeColor: Int = 0xFFFFFF
    private var strokeWidth: Float = 0F

    override fun onDraw(canvas: Canvas?) {
        val rawBitmap: Bitmap? = getBitMap(drawable)
        if (rawBitmap != null) {
            val viewMinSize: Int = width.coerceAtMost(height)// 取较小的值
            val viewWidth: Float = viewMinSize.toFloat()
            val viewHeight: Float = viewMinSize.toFloat()
            if (mShader == null || !rawBitmap.equals(mRawBitmap)) {
                mRawBitmap = rawBitmap
                mShader = BitmapShader(mRawBitmap!!, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
            }
            if (mShader != null) {
                mMatrix.setScale(viewWidth / rawBitmap.width, viewHeight / rawBitmap.height)
                mShader?.setLocalMatrix(mMatrix)
            }
            mPaintBitmap.shader = mShader
            val radius: Float = viewMinSize / 2.0f

            if (strokeWidth != 0f) {
                val whitePaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
                whitePaint.color = strokeColor
                canvas?.drawCircle(radius, radius, radius, whitePaint)
                canvas?.drawCircle(radius, radius, radius - strokeWidth, mPaintBitmap)
            } else {
                canvas?.drawCircle(radius, radius, radius, mPaintBitmap)
            }
        } else {
            super.onDraw(canvas)
        }
    }

    /**
     * @param drawable 要绘制的素材
     */
    private fun getBitMap(drawable: Drawable?): Bitmap? {
        if (drawable != null) {
            when (drawable::class.java) {
                BitmapDrawable::class.java -> return drawable.toBitmap()
                ColorDrawable::class.java -> {
                    val rect: Rect = drawable.bounds
                    val width: Int = rect.right - rect.left
                    val height: Int = rect.bottom - rect.top
                    val drawableColor: ColorDrawable = drawable as ColorDrawable
                    val color: Int = drawableColor.color
                    val bitmap: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
                    val canvas: Canvas = Canvas(bitmap)
                    canvas.drawARGB(
                            Color.alpha(color), Color.red(color), Color.green(color), Color.blue(
                            color
                    )
                    )
                    return bitmap
                }
                else -> return null
            }
        }
        return null
    }

    /**
     * @param strokeWidth 边框宽度
     */
    public fun setStrokeWidth(strokeWidth: Float) {
        this.strokeWidth = strokeWidth
    }

    /**
     * @param strokeColor 边框颜色
     */
    public fun setStrokeColor(strokeColor: Int) {
        this.strokeColor = strokeColor
    }
}

class MainActivity : AppCompatActivity() {
    //当前布局判断
    private val main_layout_home=0
    private val main_layout_msg=1
    private val main_layout_myinfo=2
    private var main_now_layout:Int=main_layout_home
    lateinit var view: LinearLayout
    lateinit var myinfoview:android.view.View
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (getSupportActionBar()!=null){//去掉标题
            getSupportActionBar()?.hide()
        };
        setContentView(R.layout.activity_main)

        //主界面layout
        view=this.findViewById(R.id.main_layout)
        //主页
        val homeview=LayoutInflater.from(this).inflate(R.layout.activity_home, null)
        //聊天页面
        val msgview=LayoutInflater.from(this).inflate(R.layout.search1, null)
        //个人页面
        myinfoview=LayoutInflater.from(this).inflate(R.layout.activity_myinfo, null)
        //显示主页
        view.addView(homeview, view.layoutParams)
        main_now_layout=main_layout_home
        home_listener(homeview)
        //切换登录
        val inte=Intent(this, login1::class.java)
        startActivity(inte)

        //监听菜单按钮
        val btn_home:Button=this.findViewById(R.id.btn_home)
        val btn_msg:Button=this.findViewById(R.id.btn_msg)
        val btn_myinfo:Button=this.findViewById(R.id.btn_myinfo)

        btn_home.setOnClickListener { v->
            //如果当前不在目标界面，切换到目标界面
            if(main_now_layout!=main_layout_home){
                view.removeAllViews()
                view.addView(homeview, view.layoutParams)
                view.setBackgroundColor(Color.TRANSPARENT)
                main_now_layout=main_layout_home
                home_listener(homeview)
            }
        }

        btn_msg.setOnClickListener { v->
            //如果当前不在目标界面，切换到目标界面
            if(main_now_layout!=main_layout_msg){
                view.removeAllViews()
                view.addView(msgview, view.layoutParams)
                view.setBackgroundColor(Color.WHITE)
                main_now_layout=main_layout_msg
                msg_listener(msgview)
            }
        }

        btn_myinfo.setOnClickListener { v->
            //如果当前不在目标界面，切换到目标界面
            if(main_now_layout!=main_layout_myinfo){
                view.removeAllViews()
                view.addView(myinfoview, view.layoutParams)
                view.setBackgroundColor(Color.WHITE)
                main_now_layout=main_layout_myinfo
                myinfo_listener(myinfoview)//设置监听器
            }
        }
        //
        /*val myinfo_layout_classroom:LinearLayout=myinfoview.findViewById(R.id.myinfo_layout_classroom)
        myinfo_layout_classroom.setOnClickListener{v->
            //myinfo_layout_classroom.setBackgroundColor(Color.GRAY)
        }*/


    }

      //监听主页
    fun home_listener(myview: android.view.View){
        //监听主页的按钮
        val home_btn_errors:Button=myview.findViewById(R.id.home_btn_errors)
        val home_btn_book:Button=myview.findViewById(R.id.home_btn_book)
        val home_btn_database:Button=myview.findViewById(R.id.home_btn_database)
        val home_btn_cho_qubank:Button=myview.findViewById(R.id.home_btn_cho_qubank)
        val home_sign:Button=myview.findViewById(R.id.home_sign)
        val home_icon:testMyOvalImageView=myview.findViewById(R.id.home_icon)
        //val testtext:TextView=myview.findViewById(R.id.testtext)
          home_btn_database.text= define_home_value
          server_queues =Volley.newRequestQueue(getApplicationContext())

        home_btn_errors.setOnClickListener { v->
            //进入错题集页面
            val intent1=Intent(this, errors::class.java)
            startActivity(intent1)
        }
        home_btn_book.setOnClickListener { v->
            //进入笔记页面
            val intent1=Intent(this, study_book::class.java)
            startActivity(intent1)
        }
          home_btn_database.setOnClickListener { v->
            //进入当前题库界面
            startActivity(Intent(this, study_qu::class.java))
        }
        home_btn_cho_qubank.setOnClickListener { v->
            //进入选择题库界面（右上角）
            val inent=Intent(this, study_choice_database::class.java)
            startActivity(inent)
            home_btn_database.text= define_home_value
            //startActivity(Intent(this, study_choice_database::class.java))
        }
        home_sign.setOnClickListener { v->
            //签到计数器加一，并上传到服务器
            define_sign_times+=1
            home_sign.text="已签到"+(define_sign_times).toString()+"天"
        }
        home_icon.setOnClickListener { v->
            //切换到我界面
            view.removeAllViews()
            view.addView(myinfoview, view.layoutParams)
            view.setBackgroundColor(Color.WHITE)
            main_now_layout=main_layout_myinfo
            myinfo_listener(myinfoview)//设置监听器
        }
    }
    //聊天页面
    fun msg_listener(myview: android.view.View){
        val search_btn:Button=myview.findViewById(R.id.search_btn)  //发送按钮
        val msg: EditText =myview.findViewById(R.id.search_text)   //文本框

        search_btn.setOnClickListener { v->
            Toast.makeText(myview.context,"未搜索到联系人",Toast.LENGTH_SHORT)
                .show()
            startActivity(Intent(this,mymsg::class.java))
        }
    }

    //我页面
    fun myinfo_listener(myview: android.view.View){
        val myinfo_setting:Button=myview.findViewById(R.id.myinfo_setting)
        val myinfo_layout_errors:LinearLayout=myview.findViewById(R.id.myinfo_layout_errors)
        val myinfo_layout_book:LinearLayout=myview.findViewById(R.id.myinfo_layout_book)
        val myinfo_layout_help:LinearLayout=myview.findViewById(R.id.myinfo_layout_help)
        val myinfo_layout_set:LinearLayout=myview.findViewById(R.id.myinfo_layout_set)
        val myinfo_signup_times:TextView=myview.findViewById(R.id.myinfo_signup_times)
        myinfo_signup_times.text= define_sign_times.toString()
        myinfo_setting.setOnClickListener{ v->
            //进入设置界面
            start_setting()
        }
        myinfo_layout_errors.setOnClickListener { v->
            //进入错题集页面
            val intent1=Intent(this, errors::class.java)
            startActivity(intent1)
        }
        myinfo_layout_book.setOnClickListener { v->
            //进入笔记页面
            val intent1=Intent(this, study_book::class.java)
            startActivity(intent1)
        }
        myinfo_layout_help.setOnClickListener { v->
            //进入帮助与反馈
            val intent1=Intent(this, myinfo_help::class.java)
            startActivity(intent1)
        }
        myinfo_layout_set.setOnClickListener { v->
            //进入设置界面
            start_setting()
        }
    }

    //切入设置布局
    fun start_setting(){
        //首先加载布局文件
        define_study_sub_arg1=layout_class.LAYOUT_CLASS_SETTING //加载设置窗口
        val intent=Intent(this, study_sub::class.java)
        startActivity(intent)
    }

}